<template>
    <div class="login-container">
        <el-card class="login-card">
            <h2 class="login-title">管理后台登录</h2>

            <!-- 用户类型切换 -->
            <el-tabs v-model="userType" class="login-tabs">
                <el-tab-pane label="用户" name="user"></el-tab-pane>
                <el-tab-pane label="管理员" name="admin"></el-tab-pane>
            </el-tabs>

            <!-- 用户名和密码输入框 -->
            <el-input v-model="username" placeholder="请输入用户名" class="login-input" prefix-icon="el-icon-user" />
            <el-input v-model="password" type="password" placeholder="请输入密码" class="login-input"
                prefix-icon="el-icon-lock" />

            <!-- 登录按钮 -->
            <el-button type="primary" class="login-btn" @click="login">登录</el-button>
            <el-text v-if="userType == 'user'" @click="onResgister">没有账号？点此<el-text
                    type="primary">注册</el-text></el-text>

        </el-card>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/userStore';
import { ElMessage } from 'element-plus';  // 引入 el-message
import axios from 'axios';

const username = ref('');
const password = ref('');
const userType = ref('user');  // 默认选择用户
const router = useRouter();
const userStore = useUserStore();

const login = async () => {
    if (!username.value || !password.value) {
        // 如果用户名或密码为空，使用 el-message 显示提示
        ElMessage.error('请输入用户名和密码');
        return;
    }

    if (username.value && password.value && userType.value === 'admin') {
        try {
            axios.post('http://localhost:3000/admin/auth/login', {
                username: username.value,
                password: password.value
            })
                .then(response => {
                    console.log('登录成功:', response.data);
                    localStorage.setItem('token', response.data.token);
                    router.push('/dashboard');
                })
                .catch(error => {
                    console.error('登录请求错误:', error.response.data);
                    ElMessage.error('登录失败');
                });

        } catch (error) {
            ElMessage.error('登录请求失败');
            console.error('登录请求错误:', error);
        }
    } else if (username.value && password.value && userType.value === 'user') {
        ElMessage.error('啊哦~我还没写用户页面呢');
    }
};
const onResgister = () => {
    router.push('/register')
}
</script>

<style scoped>
.login-container {
    display: flex;
    height: 100vh;
    justify-content: center;
    align-items: center;
    background: #f5f5f5;
}

.login-card {
    padding: 30px;
    width: 350px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
}

.login-title {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 20px;
}

.login-tabs {
    margin-bottom: 20px;
}

.login-input {
    margin-bottom: 15px;
}

.login-btn {
    width: 100%;
    margin-top: 20px;
}
</style>